package com.swind.fast.modules.app.industry.service.impl;

import com.swind.fast.modules.app.industry.entity.ScsDevice;
import com.swind.fast.modules.app.industry.entity.ScsDeviceProp;
import com.swind.fast.modules.app.industry.entity.ScsDeviceDataLog;
import com.swind.fast.modules.app.industry.mapper.ScsDevicePropMapper;
import com.swind.fast.modules.app.industry.mapper.ScsDeviceDataLogMapper;
import com.swind.fast.modules.app.industry.mapper.ScsDeviceMapper;
import com.swind.fast.modules.app.industry.service.IScsDeviceService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 设备表

 * @Date:   2021-01-06
 * @Version: V1.0
 */
@Service
public class ScsDeviceServiceImpl extends ServiceImpl<ScsDeviceMapper, ScsDevice> implements IScsDeviceService {

	@Autowired
	private ScsDeviceMapper scsDeviceMapper;
	@Autowired
	private ScsDevicePropMapper scsDevicePropMapper;
	@Autowired
	private ScsDeviceDataLogMapper scsDeviceDataLogMapper;

	@Override
	@Transactional
	public void saveMain(ScsDevice scsDevice, List<ScsDeviceProp> scsDevicePropList,List<ScsDeviceDataLog> scsDeviceDataLogList) {
		scsDeviceMapper.insert(scsDevice);
		if(scsDevicePropList!=null && scsDevicePropList.size()>0) {
			for(ScsDeviceProp entity:scsDevicePropList) {
				//外键设置
				entity.setDeviceId(scsDevice.getId());
				scsDevicePropMapper.insert(entity);
			}
		}
		if(scsDeviceDataLogList!=null && scsDeviceDataLogList.size()>0) {
			for(ScsDeviceDataLog entity:scsDeviceDataLogList) {
				//外键设置
				entity.setDeviceId(scsDevice.getId());
				scsDeviceDataLogMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ScsDevice scsDevice,List<ScsDeviceProp> scsDevicePropList,List<ScsDeviceDataLog> scsDeviceDataLogList) {
		scsDeviceMapper.updateById(scsDevice);

		//1.先删除子表数据
		scsDevicePropMapper.deleteByMainId(scsDevice.getId());
		// scsDeviceDataLogMapper.deleteByMainId(scsDevice.getId());

		//2.子表数据重新插入
		if(scsDevicePropList!=null && scsDevicePropList.size()>0) {
			for(ScsDeviceProp entity:scsDevicePropList) {
				//外键设置
				entity.setDeviceId(scsDevice.getId());
				scsDevicePropMapper.insert(entity);
			}
		}
		// if(scsDeviceDataLogList!=null && scsDeviceDataLogList.size()>0) {
		// 	for(ScsDeviceDataLog entity:scsDeviceDataLogList) {
		// 		//外键设置
		// 		entity.setDeviceId(scsDevice.getId());
		// 		scsDeviceDataLogMapper.insert(entity);
		// 	}
		// }
	}

	@Override
	@Transactional
	public void delMain(String id) {
		scsDevicePropMapper.deleteByMainId(id);
		scsDeviceDataLogMapper.deleteByMainId(id);
		scsDeviceMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			scsDevicePropMapper.deleteByMainId(id.toString());
			scsDeviceDataLogMapper.deleteByMainId(id.toString());
			scsDeviceMapper.deleteById(id);
		}
	}

}
